'use strict';
const Service = require('egg').Service;

class OrderController extends Service {
  async postorder (data) {
    console.log('提交订单：', data);
    await this.app.mysql.insert('order1', data);
    return {
      code: 200,
      message: '订单提交成功',
    };
  }
  async getuserorder (data) {
    console.log('获取用户订单：', data);
    const res = await this.app.mysql.select('order1', { where: { users_id: data.users_id } });
    return res;
  }
  // 联表查询的用户订单信息
  async getuserorderinfo (data) {
    console.log('获取订单信息：', data);
    const sql = 'select * from products as a right join order1 as b on a.product_id = b.product_id where order_id = ?';
    const content = data.order_id;
    const res = await this.app.mysql.query(sql, content);
    return res;
  }
  async getorders (data) {
    console.log('获取订单：', data);
    let sql = 'select * from order1';
    const content = [];// 参数
    let isMore = false;// 是否有多个查询参数,只用于搜索查询
    if (data.order_id) {
      sql += ' where order_id = ?';
      content.push(data.order_id);
    }
    if (data.product_idORname) {
      // sql += ' where concat(ifnull(order_id,``),ifnull(product_name,``)) like ?';
      sql += ' where concat(order_id,product_name) like ?';
      content.push('%' + data.product_idORname + '%');
      isMore = true;
    }
    if (data.goods_type) {
      if (isMore) { // true代表有多个参数
        sql += 'and goods_type LIKE ?';// and是两个条件都必须满足，or是或的关系
      } else {
        sql += ' WHERE goods_type LIKE ?';
      }
      content.push('%' + data.goods_type + '%');
      isMore = true;
    }
    if (data.begin && data.end) {
      if (isMore) { // true代表有多个参数
        sql += 'and create_time between ? and ?';// and是两个条件都必须满足，or是或的关系
      } else {
        sql += ' WHERE create_time between ? and ?';
      }
      content.push(data.begin, data.end);
      isMore = true;
    }
    // 开启分页
    if (data.current && data.pageSize) {
      const current = data.current;// 当前页码
      const pageSize = data.pageSize;// 一页展示多少条数据
      sql += ' LIMIT ?,?';
      content.push(parseInt((current - 1) * pageSize), parseInt(pageSize));
    }
    const res = {
      code: 200,
      page: data.current,
      pageSize: data.pageSize,
      data: [],
      total: '',
    };
    res.data = await this.app.mysql.query(
      sql, content
    );
    res.total = await this.app.mysql.count('order1');
    return res;

  }
  // 客户订单
  async getclientorders (data) {
    console.log('客户订单', data);
    const res = await this.app.mysql.query('select * from products as a right join order1 as b on a.product_id = b.product_id where a.users_id = ? ORDER BY b.create_time desc', [data.users_id]);
    return res;
  }
  // 管理员订单审核
  async auditorder (data) {
    console.log('审核订单：', data);
    const where = {
      where: {
        order_id: data.order_id,
      },
    };
    const res = await this.app.mysql.update('order1', { order_status: data.order_status }, where);
    return res;
  }
  // 管理员订单修改\企业发货
  async updateorder (data) {
    console.log('修改订单：', data);
    const where = {
      where: {
        order_id: data.order_id,
      },
    };
    await this.app.mysql.update('order1', data, where);
    return {
      code: 200,
      message: '订单修改成功',
    };
  }
  // 管理员订单删除
  async delorder (data) {
    console.log('删除订单：', data);
    const where = {
      order_id: data.order_id,
    };
    const res = await this.app.mysql.delete('order1', where);
    return res;
  }
  // 物流信息搜索
  async mapsearch (data) {
    const obj1 = await this.app.mysql.select('order1', {
      where: {
        users_id: data.users_id,
        product_name: data.product_name,
      },
    });
    const obj2 = await this.app.mysql.select('products', { where: { product_name: data.product_name } });
    console.log(obj1, obj2);
    if (obj1.length === 0 || obj2.length === 0) {
      return false;
    }
    const res = Object.assign(obj1[0], obj2[0]);
    return res;
  }
  // 后台地图数据（订单）
  async getmapdata (data) {
    const sql = 'select * from products as a right join order1 as b on a.product_id = b.product_id';
    const mapdata = await this.app.mysql.query(sql);
    const status1_sql = "select COUNT(*) as sum from order1 where order_status is null OR order_status = '0' OR order_status = '1'";
    const status1 = await this.app.mysql.query(status1_sql);
    const status2_sql = "select COUNT(*) as sum from order1 where order_status = '2'";
    const status2 = await this.app.mysql.query(status2_sql);
    const status3_sql = "select COUNT(*) as sum from order1 where order_status = '3'";
    const status3 = await this.app.mysql.query(status3_sql);
    return {
      code: 200,
      mapdata,
      status1: status1[0].sum,
      status2: status2[0].sum,
      status3: status3[0].sum,
    };
  }
}
module.exports = OrderController;
